package com.yc.hotel.controller;

import java.util.List;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.hotel.dto.UserInfoDTO;
import com.yc.hotel.dto.UserItemDTO;
import com.yc.hotel.enums.ResultEnum;
import com.yc.hotel.user.feign.IUserItemFeignClient;
import com.yc.hotel.util.ConstantInfo;
import com.yc.hotel.vo.ResultVO;

@RestController
@RequestMapping("/userItem")
public class UserItemFeignController {

	@Autowired 
	private IUserItemFeignClient userItemFeignClient;

	@GetMapping("/findByUno")
	private ResultVO findByUno(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfo.CURRENTMEMBERINFO);
		if (obj == null) {
			return null;
		}
		UserInfoDTO user = (UserInfoDTO)obj;
		int uno = user.getUno();
		List<UserItemDTO> userItems = userItemFeignClient.findByUno(uno);
		
		return new ResultVO(ResultEnum.SUCCESS, userItems);
	}
	
	@PostMapping("/add")
	private ResultVO add(UserItemDTO userItem, HttpSession session) {
		Object obj = session.getAttribute(ConstantInfo.CURRENTMEMBERINFO);
		if (obj == null) {
			return null;
		}
		UserInfoDTO user = (UserInfoDTO)obj;
		int uno = user.getUno();
		
		int result = userItemFeignClient.add(userItem, uno);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS, result);
	}
}
